function r51_initializer(ncid) result (ids)

    use iasing_osse
    use netcdf

    implicit none
    ! Arguments
    integer         :: ncid
    type(r4a_ids)   :: ids
    ! netcdf status variable
    integer :: io

    ! Dimension ids 
    io = nf90_inq_dimid(ncid, "atmosphere", ids%atm_did)
    if (io /= NF90_NOERR) then
        print*, "Error reading 'atmosphere' dimension id..."
        print*, nf90_strerror(io)
        print*
        ids%atm_did = io
    endif
    io = nf90_inq_dimid(ncid, "level", ids%lev_did)
    if (io /= NF90_NOERR) then
        print*, "Error reading 'level' dimension id..."
        print*, nf90_strerror(io)
        print*
        ids%lev_did = io
    endif
    io = nf90_inq_dimid(ncid, "wnu_iasi", ids%wia_did)
    if (io /= NF90_NOERR) then
        print*, "Error reading 'wnu_iasi' dimension id..."
        print*, nf90_strerror(io)
        print*
        ids%wia_did = io
    endif
    io = nf90_inq_dimid(ncid, "wnu_iang", ids%wng_did)
    if (io /= NF90_NOERR) then
        print*, "Error reading 'wnu_iang' dimension id..."
        print*, nf90_strerror(io)
        print*
        ids%wng_did = io
    endif
    io = nf90_inq_dimid(ncid, "ObsChannelList", ids%ocl_did)
    if (io /= NF90_NOERR) then
        print*, "Error reading 'ObsChannelList' dimension id..."
        print*, nf90_strerror(io)
        print*
        ids%ocl_did = io
    endif

    ! Dimensions group
    io = nf90_inq_ncid(ncid, "Dimensions", ids%dim%gid)
    if (io == NF90_NOERR) then
        io = nf90_inq_varid(ids%dim%gid, "atmosphere", ids%dim%atm)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'atmosphere' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%dim%atm = io
        endif
        io = nf90_inq_varid(ids%dim%gid, "level", ids%dim%lev)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'level' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%dim%lev = io
        endif
        io = nf90_inq_varid(ids%dim%gid, "wnu_iasi", ids%dim%wia)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'wnu_iasi' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%dim%wia = io
        endif
        io = nf90_inq_varid(ids%dim%gid, "wnu_iang", ids%dim%wng)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'wnu_iang' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%dim%wng = io
        endif
        io = nf90_inq_varid(ids%dim%gid, "ObsChannelList", ids%dim%ocl)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'ObsChannelList' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%dim%ocl = io
        endif
    else
        print*, "Error reading 'Dimensions' group id..."
        print*, nf90_strerror(io)
        print*
        ids%dim%gid = io
        ids%dim%atm = io
        ids%dim%lev = io
        ids%dim%wia = io
        ids%dim%wng = io
        ids%dim%ocl = io
    endif
    

    ! Coordinates group
    io = nf90_inq_ncid(ncid, "Coordinates", ids%coo%gid)
    if (io == NF90_NOERR) then
        io = nf90_inq_varid(ids%coo%gid, "lon", ids%coo%lon)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'lon' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%coo%lon = io
        endif
        io = nf90_inq_varid(ids%coo%gid, "lat", ids%coo%lat)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'lat' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%coo%lat = io
        endif
        io = nf90_inq_varid(ids%coo%gid, "ele", ids%coo%ele)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'ele' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%coo%ele = io
        endif
        io = nf90_inq_varid(ids%coo%gid, "dat", ids%coo%dat)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'dat' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%coo%dat = io
        endif
        io = nf90_inq_varid(ids%coo%gid, "tim", ids%coo%tim)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'tim' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%coo%tim = io
        endif
    else
        print*, "Error reading 'Coordinates' group id..."
        print*, nf90_strerror(io)
        print*
        ids%coo%gid = io
        ids%coo%lon = io
        ids%coo%lat = io
        ids%coo%ele = io
        ids%coo%dat = io
        ids%coo%tim = io
    endif

    ! Observation parameters group
    io = nf90_inq_ncid(ncid, "Observation parameters", ids%opa%gid)
    if (io == NF90_NOERR) then
        io = nf90_inq_varid(ids%opa%gid, "psu", ids%opa%psu)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'psu' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%opa%psu = io
        endif
        io = nf90_inq_varid(ids%opa%gid, "tsu", ids%opa%tsu)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'tsu' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%opa%tsu = io
        endif
        io = nf90_inq_varid(ids%opa%gid, "emi", ids%opa%emi)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'emi' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%opa%emi = io
        endif
        io = nf90_inq_varid(ids%opa%gid, "iaa", ids%opa%iaa)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'iaa' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%opa%iaa = io
        endif
        io = nf90_inq_varid(ids%opa%gid, "iza", ids%opa%iza)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'iza' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%opa%iza = io
        endif
        io = nf90_inq_varid(ids%opa%gid, "sza", ids%opa%sza)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'sza' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%opa%sza = io
        endif
        io = nf90_inq_varid(ids%opa%gid, "saa", ids%opa%saa)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'saa' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%opa%saa = io
        endif
        io = nf90_inq_varid(ids%opa%gid, "lsm", ids%opa%lsm)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'lsm' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%opa%lsm = io
        endif
        io = nf90_inq_varid(ids%opa%gid, "clc", ids%opa%clc)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'clc' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%opa%clc = io
        endif
        io = nf90_inq_varid(ids%opa%gid, "ert", ids%opa%ert)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'ert' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%opa%ert = io
        endif
        io = nf90_inq_varid(ids%opa%gid, "rst", ids%opa%rst)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'rst' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%opa%ert = io
        endif
    else
        print*, "Error reading 'Observation parameters' group id..."
        print*, nf90_strerror(io)
        print*
        ids%opa%gid = io
        ids%opa%psu = io
        ids%opa%tsu = io
        ids%opa%emi = io
        ids%opa%iza = io
        ids%opa%sza = io
        ids%opa%saa = io
        ids%opa%lsm = io
        ids%opa%clc = io
        ids%opa%ert = io
    endif

    ! Vertical profiles group
    io = nf90_inq_ncid(ncid, "Vertical profiles", ids%vpr%gid)
    if (io == NF90_NOERR) then
        io = nf90_inq_varid(ids%vpr%gid, "tem", ids%vpr%tem)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'tem' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%vpr%tem = io
        endif
        io = nf90_inq_varid(ids%vpr%gid, "q", ids%vpr%q)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'q' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%vpr%q   = io
        endif
        io = nf90_inq_varid(ids%vpr%gid, "co2", ids%vpr%co2)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'co2' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%vpr%co2 = io
        endif
        io = nf90_inq_varid(ids%vpr%gid, "o3", ids%vpr%o3)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'o3' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%vpr%o3  = io
        endif
        io = nf90_inq_varid(ids%vpr%gid, "co", ids%vpr%co)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'co' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%vpr%co  = io
        endif
        io = nf90_inq_varid(ids%vpr%gid, "ch4", ids%vpr%ch4)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'ch4' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%vpr%ch4 = io
        endif
        io = nf90_inq_varid(ids%vpr%gid, "so2", ids%vpr%so2)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'so2' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%vpr%so2 = io
        endif
    else
        print*, "Error reading 'Vertical profiles' group id..."
        print*, nf90_strerror(io)
        print*
        ids%vpr%gid = io
        ids%vpr%tem = io
        ids%vpr%q   = io
        ids%vpr%co2 = io
        ids%vpr%o3  = io
        ids%vpr%co  = io
        ids%vpr%ch4 = io
        ids%vpr%so2 = io
    endif

    ! Cloud information group
    io = nf90_inq_ncid(ncid, "Cloud information", ids%cld%gid)
    if (io == NF90_NOERR) then
        io = nf90_inq_varid(ids%cld%gid, "cc", ids%cld%cc)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'cc' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%cld%cc = io
        endif
        io = nf90_inq_varid(ids%cld%gid, "iwc", ids%cld%iwc)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'iwc' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%cld%cc = io
        endif
        io = nf90_inq_varid(ids%cld%gid, "lwc", ids%cld%lwc)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'lwc' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%cld%cc = io
        endif
        io = nf90_inq_varid(ids%cld%gid, "rwc", ids%cld%rwc)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'rwc' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%cld%cc = io
        endif
        io = nf90_inq_varid(ids%cld%gid, "swc", ids%cld%swc)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'swc' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%cld%cc = io
        endif
    else
        print*, "Error reading 'Cloud information' group id..."
        print*, nf90_strerror(io)
        print*
        ids%cld%gid = io
        ids%cld%cc  = io
        ids%cld%iwc = io
        ids%cld%lwc = io
        ids%cld%rwc = io
        ids%cld%swc = io
    endif
    

    ! Radiances group
    io = nf90_inq_ncid(ncid, "Radiances", ids%rbt%gid)
    if (io == NF90_NOERR) then
        io = nf90_inq_varid(ids%rbt%gid, "ria", ids%rbt%ria)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'ria' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%rbt%ria = io
        endif
        io = nf90_inq_varid(ids%rbt%gid, "rng", ids%rbt%rng)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'rng' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%rbt%rng = io
        endif
        io = nf90_inq_varid(ids%rbt%gid, "ngb", ids%rbt%ngb)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'ngb' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%rbt%ngb = io
        endif
    else
        print*, "Error reading 'Radiances' group id..."
        print*, nf90_strerror(io)
        print*
        ids%rbt%gid = io
        ids%rbt%ria = io
        ids%rbt%rng = io
        ids%rbt%ngb = io
    endif

    ! Observations group
    io = nf90_inq_ncid(ncid, "Observations", ids%obs%gid)
    if (io == NF90_NOERR) then
        io = nf90_inq_varid(ids%obs%gid, "oma", ids%obs%oma)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'oma' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%obs%oma = io
        endif
        io = nf90_inq_varid(ids%obs%gid, "obt", ids%obs%obt)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'obt' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%obs%obt = io
        endif
        io = nf90_inq_varid(ids%obs%gid, "obi", ids%obs%obi)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'obi' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%obs%obi = io
        endif
        io = nf90_inq_varid(ids%obs%gid, "ofd", ids%obs%ofd)
        if (io /= NF90_NOERR) then
            print*, "Error reading 'ofd' variable id..."
            print*, nf90_strerror(io)
            print*
            ids%obs%ofd = io
        endif
    else
        print*, "Error reading 'Observations' group id..."
        print*, nf90_strerror(io)
        print*
        ids%obs%gid = io
        ids%obs%oma = io
        ids%obs%obt = io
        ids%obs%obi = io
        ids%obs%ofd = io
    endif
    
    return 

end function r51_initializer
